package com.fengmi.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.fengmi.dto.PageDTO;
import com.fengmi.entity.MallGoodsBrand;
import com.fengmi.entity.SysRole;
import com.fengmi.mapper.SysRoleMapper;
import com.fengmi.service.SysRoleService;
import com.fengmi.vo.PageResultVO;
import com.fengmi.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class SysRoleServiceImpl implements SysRoleService {

    @Autowired
    private SysRoleMapper roleMapper;

    @Override
    public PageResultVO<SysRole> findRoleByPage(PageDTO<SysRole> pageDTO) {
        //非空判断
        if (pageDTO == null) {
            return new PageResultVO<>(false, "参数不合法");
        }

        //当前页
        Integer pageNum = pageDTO.getPageNum();
        //每页记录数
        Integer pageSize = pageDTO.getPageSize();
        //进行非空判断
        if (pageNum == null || pageNum <= 0) {
            pageNum = 1;
        }
        if (pageSize == null || pageSize <= 0) {
            pageSize = 5;
        }
        //开始分页
        PageHelper.startPage(pageNum, pageSize);
        //调用mapper层查询
        List<SysRole> brandList = roleMapper.findRolesByWhere(pageDTO.getWhere());
        //分页信息对象创建，放入查询集合
        PageInfo<SysRole> pageInfo = new PageInfo<>(brandList);
        //创建返回对象
        PageResultVO<SysRole> pageResultVO = new PageResultVO<>(true, "查询成功");
        //设置每页数据
        pageResultVO.setData(pageInfo.getList());
        //设置总页数
        pageResultVO.setTotalPage(pageInfo.getPages());
        //设置总记录数
        pageResultVO.setTotalSize(pageInfo.getTotal());

        return pageResultVO;
    }

    @Override
    public ResultVO updateRoleEnable(String roleCode, Integer enable) {
        if (StringUtils.isEmpty(roleCode)) {
            return new ResultVO(false, "参数不合法");
        }

        int i = roleMapper.updateEnable(roleCode, enable);

        return new ResultVO(i == 1 ? true : false, i == 1 ? "修改角色可用状态成功" : "修改角色可用状态失败");
    }

    @Override
    public ResultVO saveAddRole(SysRole sysRole) {
        if (sysRole == null) {
            return new ResultVO(false, "参数不合法");
        }
        //设置角色code，数据库中code和order是联合主键，code为String类型，将他处理成自增长类型
        //查询数据库，查出所有角色信息
        List<SysRole> roleList = roleMapper.findAll();
        if (roleList == null || roleList.size() <= 0) {
            //第一次添加
            sysRole.setRoleCode("1");
        } else {
            //遍历获取所有的code，并保存下来
            String[] codeArr = new String[roleList.size()];
            for (int i = 0; i < roleList.size(); i++) {
                codeArr[i] = roleList.get(i).getRoleCode();
            }
            //获取最后一个code
            String lastCode = codeArr[codeArr.length - 1];
            //转为int类型+1
            Integer codeNum = Integer.parseInt(lastCode) + 1;
            //设置新的code
            sysRole.setRoleCode(String.valueOf(codeNum));
        }

        //设置创建时间
        sysRole.setCreateTime(new Date());
        //其他字段前端都已传，添加到数据库中
        int i = roleMapper.saveAddRole(sysRole);

        return new ResultVO(i == 1 ? true : false, i == 1 ? "添加角色成功" : "添加角色失败");
    }

    @Override
    public ResultVO saveUpdateRoleBasicInfo(SysRole sysRole) {
        if (sysRole == null) {
            return new ResultVO(false, "参数不合法");
        }

        int i = roleMapper.updateRoleBasicInfo(sysRole);
        return new ResultVO(i == 1 ? true : false, i == 1 ? "修改角色信息成功" : "修改角色信息失败");
    }

    @Override
    public ResultVO saveRolesPermissions(String roleId, String[] permissions) {
        if (StringUtils.isEmpty(roleId) || permissions == null) {
            return new ResultVO(false, "参数不合法");
        }
        //先做删除操作
        int i1 = roleMapper.deleteAllPermissionsByRoleId(roleId);
        //在做插入操作
        int i2 = roleMapper.saveRolePermissions(roleId, permissions);
        if (i1 + i2 > 0) {
            return new ResultVO(true, "角色权限分配成功");
        }
        return new ResultVO(true, "角色权限分配失败");
    }

    @Override
    public ResultVO selectPermissionsByRoleId(String roleId) {
        if (StringUtils.isEmpty(roleId)) {
            return new ResultVO(false, "参数不合法");
        }
        List<String> permissions = roleMapper.findAllPermissionsByRoleId(roleId);

        return new ResultVO(true, "success", permissions);
    }

    @Override
    public ResultVO selectAllRoles() {
        List<SysRole> roleList = roleMapper.findAll();
        return new ResultVO(true, "success", roleList);
    }
}
